package com.comdosoft.comdoandroidbase.leftmenu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.comdosoft.comdoandroidbase.R;
import com.comdosoft.comdoandroidbase.adapter.LeftMenuAdapter;
import com.comdosoft.comdoandroidbase.util.DataUtil;

/**
 * Created by yewei on 2015/9/16.
 */

public class LeftMenuFragment extends Fragment {

    private ListView lvMenu;
    private LeftMenuAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.leftmenufragment, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        lvMenu = (ListView) view.findViewById(R.id.lv_menu);
        adapter = new LeftMenuAdapter(getActivity(), DataUtil.getDataList());
        lvMenu.setAdapter(adapter);
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectedIndex(position);

                if (getActivity() instanceof OnLeftMenuItemClickListener) {
                    ((OnLeftMenuItemClickListener) getActivity()).onItemClick(adapter.getItem(position));
                }
            }
        });

        //触发点击第一个Item
        lvMenu.performItemClick(lvMenu.getChildAt(0), 0, lvMenu.getItemIdAtPosition(0));
    }


    public interface OnLeftMenuItemClickListener {
        public void onItemClick(Object itemData);
    }
}
